﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ArticleList : System.Web.UI.Page
{
    protected void ArticleList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.ArticleList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void ArticleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    }

    protected void ArticleList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int num = e.Row.DataItemIndex - (this.ArticleList.PageIndex * this.ArticleList.PageSize);
        if (num >= 0)
        {
            string str = this.ArticleList.DataKeys[num].Values[1].ToString();
            Button button = (Button)e.Row.FindControl("DelBt");
            HyperLink link = (HyperLink)e.Row.FindControl("EditHyperLink");
            if (button != null)
            {
                if (!ManagerLogin.ValidateRole(string.Format("1_{0}_b", str)))
                {
                    button.Enabled = false;
                }
                if (!ManagerLogin.ValidateRole(string.Format("1_{0}_a", str)))
                {
                    link.Enabled = false;
                }
            }
        }
    }

    protected void ArticleList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.ArticleList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Delete("article", string.Format("ArticleId={0}", str));
        db.Close();
        this.Bind();
        this.Msg.Text = "文章删除成功！";
        this.Msg.Visible = true;
    }

    protected void ArticleList_RowEditing(object sender, GridViewEditEventArgs e)
    {
    }

    protected void ArticleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }

    protected void ProductsLock_CheckedChanged(object sender, EventArgs e)//推荐
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        CheckBox ckeSelect = null;
        for (int i = 0; i < ArticleList.Rows.Count; i++)
        {
            ckeSelect = (CheckBox)ArticleList.Rows[i].FindControl("ProductsLock");
            string str = (ckeSelect.Checked == true ? "True" : "False");
            string strwhere = string.Format("ArticleId ={0}", ArticleList.Rows[i].Cells[0].Text);
            string[,] includeColumns = new string[,] { { "ArtTJ", str, "bit", "0" } };
            db.Update("article", includeColumns, strwhere);
        }
        db.Close();
        this.Bind();
    }

    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string str2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            string str3;
            this.ArticleLink.NavigateUrl = "Article.aspx?menuId=" + str;
            this.MenuName.Text = str2;
            IDb db = new DbFactory().DriverDb();
            db.Open();
            DataTable dataTable = db.DataTable("OrderId,ClassName,FileName,ParentId,ArticleClassId", "ArticleClass", string.Format("MenuId={0}", str), "OrderId ASC");
            if (this.IsSearch.Value == "1")
            {
                string parentId = this.ClassList.SelectedValue.ToString();
                string str5 = Function.ChildIdList(dataTable, "ArticleClassId", "ParentId", parentId, true);
                str3 = string.Format("MenuId={0} AND ArticleClassId IN({1})", str, str5);
                string text = this.ArticleTitle.Text;
                if (!((text == "") | (text == null)))
                {
                    str3 = string.Format("{0} AND (Title LIKE '%{1}%' OR ArticleContent LIKE '%{1}%')", str3, text);
                }
            }
            else
            {
                str3 = string.Format("MenuId={0}", str);
            }
            DataTable table2 = db.DataTable("ClassName,ArticleClassId,ArticleId,Title,Author,AddTime,ArtTJ", "ArticleList", str3, "Weight Asc,AddTime DESC");
            db.Close();
            this.ArticleList.DataSource = table2;
            this.ArticleList.DataBind();
            dataTable.Columns.Add("ClassNameText");
            DataTable newTable = dataTable.Clone();
            Function.PopulateTable(dataTable, newTable, "ClassName", "ArticleClassId", "ParentId", 0, 0);
            if (!base.IsPostBack)
            {
                this.ClassList.DataSource = newTable;
                this.ClassList.DataTextField = "ClassNameText";
                this.ClassList.DataValueField = "ArticleClassId";
                this.ClassList.DataBind();
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        this.IsSearch.Value = "1";
        this.Bind();
    }
}
